package com.guochenglang.www.service.impl;

import com.guochenglang.www.dao.DialogDao;
import com.guochenglang.www.pojo.Dialog;
import com.guochenglang.www.service.DialogService;
import com.guochenglang.www.service.anotate.Affair;
import com.guochenglang.www.util.MyDependency;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

/**
 * 私信业务
 *
 * @author GuoChenglang
 */
public class DialogServiceImpl implements DialogService {
    private static final DialogDao DIALOG_DAO =MyDependency.returnObject(DialogDao.class);
    /**
     * 获得未读的会话
     *
     * @param userId 用户id
     * @return 未读会话列表
     */
    @Override
    public ArrayList<Dialog> dialogNoRead(Integer userId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        return DIALOG_DAO.dialogSelect(new Dialog(null, 0, null, userId, null));
    }

    /**
     * 获得未读个数
     *
     * @param userId 用户id
     * @return 未读会话的个数
     */
    @Override
    public int dialogNoReadCount(Integer userId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //封装条件
        Dialog condition = new Dialog();
        condition.setAnotherId(userId);
        condition.setIsRead(0);
        return DIALOG_DAO.dialogSelect(condition).size();
    }

    /**
     * 获得聊天记录
     *
     * @param userId    用户id
     * @param anotherId 另一个用户id
     * @return 聊天记录
     */
    @Override
    public ArrayList<Dialog> allDialog(Integer userId, Integer anotherId) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        ArrayList<Dialog> dialogs = new ArrayList<>();
        //获得一个人的发言记录
        ArrayList<Dialog> oneDialog = DIALOG_DAO.dialogSelect(new Dialog(null, null, null, anotherId, userId));
        dialogs.addAll(oneDialog);
        //获得另一个人的发言记录
        ArrayList<Dialog> anotherDialog = DIALOG_DAO.dialogSelect(new Dialog(null, null, null, userId, anotherId));
        dialogs.addAll(anotherDialog);
        //根据id进行排序
        Collections.sort(dialogs, new Comparator<Dialog>() {
            @Override
            public int compare(Dialog o1, Dialog o2) {
                return o1.getId() - o2.getId();
            }
        });
        return dialogs;
    }

    /**
     * 添加未读的消息
     *
     * @param userId    用户id
     * @param anotherId 另一个用户id
     * @param message   聊天信息
     * @return 主键
     */
    @Affair
    @Override
    public int noRead(Integer userId, Integer anotherId, String message) throws SQLException, InterruptedException, IllegalAccessException {
        return DIALOG_DAO.insertDialog(new Dialog(null,0,message,anotherId,userId));
    }

    /**
     * 添加已读消息
     *
     * @param useId     用户id
     * @param anotherId 另一个用户id
     * @param message   消息
     * @return 主键
     */
    @Affair
    @Override
    public int isRead(Integer useId, Integer anotherId, String message) throws SQLException, InterruptedException, IllegalAccessException {
        return DIALOG_DAO.insertDialog(new Dialog(null,1,message,anotherId,useId));
    }

    /**
     * 将未读状态变成已读状态
     *
     * @param dialogId 会话id
     * @return 更新行数
     */
    @Affair
    @Override
    public int updateDialog(Integer dialogId) throws SQLException, InterruptedException, IllegalAccessException {
        Dialog set = new Dialog();
        Dialog thisDialog = new Dialog();
        set.setIsRead(1);
        thisDialog.setId(dialogId);
        return DIALOG_DAO.updateDialog(set,thisDialog);
    }
}
